﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebServer.DBUtility
{
    public static class ObjectExtend
    {
        public static int ToInt32(this object obj)
        {
            return (obj == null || obj == DBNull.Value) ? -1 : Convert.ToInt32(obj);
        }

        //public static int? ToInt32(this object obj)
        //{
        //    return (obj == null || obj == DBNull.Value) ? null : (int?)Convert.ToInt32(obj);
        //}

        public static decimal ToDecimal(this object obj)
        {
            return (obj == null || obj == DBNull.Value) ? 0 : Convert.ToDecimal(obj);
        }

        //public static decimal? ToDecimal(this object obj)
        //{
        //    return (obj == null || obj == DBNull.Value) ? null : (decimal?)Convert.ToDecimal(obj);
        //}

        public static bool ToBoolean(this object obj)
        {
            return (obj == null || obj == DBNull.Value || Convert.ToInt32(obj) < 0) ? false : Convert.ToBoolean(obj);
        }

        //public static bool? ToBoolean(this object obj)
        //{
        //    return (obj == null || obj == DBNull.Value) ? null : (bool?)Convert.ToBoolean(obj);
        //}


        public static DateTime ToDateTime(this object obj)
        {
            return (obj == null || obj == DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(obj);
        }

        //public static DateTime? ToDateTime(this object obj)
        //{
        //    return (obj == null || obj == DBNull.Value) ? null : (DateTime?)Convert.ToDateTime(obj);
        //}

        public static byte ToByte(this object obj)
        {
            return (obj == null || obj == DBNull.Value) ? byte.MinValue : Convert.ToByte(obj);
        }
    }
}
